package com.itheima.ydd.controller;

import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.service.YddAdvimgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 广告图请求处理类
 */
@RestController
@RequestMapping("ydd_advimg")
@Slf4j
public class YddAdvimgController {

    @Autowired
    private YddAdvimgService yddAdvimgService;

    /**
     * 根据id获取对应的广告图详细
     * @param id    广告图id
     * @return      对应的广告图数据
     */
    @GetMapping("rowInfo")
    public ResultData rowInfo(Long id){
        log.info("要修改的广告位id为:{}",id);
        if(id==null) return ResultData.error("该数据不存在");
        YddAdvimg advimg = yddAdvimgService.getById(id);
        log.info("查询到的数据为:{}",advimg);
        return ResultData.ok("",advimg);
    }


    /**
     *分页条件查询 广告图,和对应广告位名称
     * @param page 页码
     * @param limit 页面条目数
     * @param yddAdvimg 查询条件,广告图名,广告图描述
     * @return 分页查询对象
     */
    @GetMapping("list")
    public ResultData getList(Long page, Long limit, YddAdvimg yddAdvimg){
        log.info("广告位分页查询条件:page={},limit={},yddAdvpos={}",page,limit,yddAdvimg);
        //获取查询结果
        PageResult pageResult = yddAdvimgService.getList(page, limit, yddAdvimg);
        log.info("查询结果为:{}",pageResult);
        return ResultData.ok("",pageResult);
    }

    /**
     * 广告图修改
     * @param yddAdvimg 广告图数据
     * @return  提示信息
     */
    @PostMapping("update")
    public ResultData update(@RequestBody YddAdvimg yddAdvimg){
        log.info("修改广告图数据:{}",yddAdvimg);
        yddAdvimgService.updateById(yddAdvimg);
        return ResultData.ok("","修改成功");
    }

    /**
     * 广告图删除
     * @param id    广告图id
     * @return  提示信息
     */
    @GetMapping("del")
    public ResultData delete(Long id){
        log.info("删除广告图id:{}",id);
        if (id==null) return ResultData.error("数据不存在");
        yddAdvimgService.removeById(id);
        return ResultData.ok("","删除成功");
    }

    /**
     * 广告图添加
     * @param yddAdvimg 广告图数据
     * @return  提示信息
     */
    @PostMapping("add")
    public ResultData add(@RequestBody YddAdvimg yddAdvimg){
        log.info("添加广告图:{}",yddAdvimg);
        if (yddAdvimg == null) return ResultData.error("无法添加空的数据");
        yddAdvimgService.save(yddAdvimg);
        return ResultData.ok("","添加成功");
    }


}
